package com.voltmao.webpro.frame.analyze;

import com.google.gson.internal.LinkedTreeMap;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.JsonUtil;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhouyang on 2018/2/12.
 * 解析类--->处理协议解析
 */
public class DTOJwlr {
    private HttpServletRequest request;

    public DTOJwlr(HttpServletRequest request) {
        this.request = request;
    }

    public interface GridCheckData {
        /**
         * 所有选择为True的选项
         *
         * @param request
         * @param tableName
         * @param ids
         * @throws WebProViewException
         */
        void checkTrueForIds(HttpServletRequest request, String tableName, String[] ids) throws WebProViewException;

        /**
         * 所有选择为False的选项
         *
         * @param request
         * @param tableName
         * @param ids
         * @throws WebProViewException
         */
        void checkFalseForIds(HttpServletRequest request, String tableName, String[] ids) throws WebProViewException;
    }

    public interface GridUpdateData {
        /**
         * @param request
         * @param tableName
         * @param row
         */
        void insertRow(HttpServletRequest request, String tableName, Map<String, Object> row);

        /**
         * @param request
         * @param tableName
         * @param row
         */
        void updateRow(HttpServletRequest request, String tableName, Map<String, Object> row);

        /**
         * @param request
         * @param tableName
         * @param row
         */
        void deleteRow(HttpServletRequest request, String tableName, Map<String, Object> row);
    }

    /**
     * 解析表格选择项
     *
     * @param gridCheckData
     * @throws WebProViewException
     */
    public void checkGridData(GridCheckData gridCheckData) throws WebProViewException {
        Map<String, Object> reqMap = HttpRequestUtil.getParameterMap(request);
        String formGrid = reqMap.get("formGrid") + "";

        List<Map> maps = JsonUtil.ToList(formGrid);
        for (int i = 0, size = maps.size(); i < size; i++) {
            Map table = maps.get(i);

            LinkedTreeMap<String, Object> checkData = JsonUtil.ToMap(table.get("checkData") + "");
            LinkedList<String> tureForIds = new LinkedList();
            LinkedList<String> falseForIds = new LinkedList();
            Iterator iter = checkData.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String id = entry.getKey().toString();
//                double trueForIds =
                if (Boolean.parseBoolean(entry.getValue() + "")) {
                    tureForIds.push(id);
                } else {
                    falseForIds.push(id);
                }
            }

            gridCheckData.checkTrueForIds(this.request, table.get("gridId") + "", (String[]) tureForIds.toArray());
            gridCheckData.checkFalseForIds(this.request, table.get("gridId") + "", (String[]) falseForIds.toArray());
        }
    }

    /**
     * 解析表格更新数据
     *
     * @param gridUpdateData
     */
    public void updateGridData(GridUpdateData gridUpdateData) {

        Map<String, Object> reqMap = HttpRequestUtil.getParameterMap(request);
        String formGrid = reqMap.get("formGrid") + "";

        List<Map> maps = JsonUtil.ToList(formGrid);
        for (int i = 0, size = maps.size(); i < size; i++) {
            Map table = maps.get(i);

//            LinkedTreeMap<String, Object> checkData = JsonUtil.ToMap(table.get("checkData") + "");
//            LinkedList<String> tureForIds= new LinkedList();
//            LinkedList<String> falseForIds= new LinkedList();
//            Iterator iter=checkData.entrySet().iterator();
//            while(iter.hasNext()){
//                Map.Entry entry=(Map.Entry) iter.next();
//                String id=entry.getKey().toString();
////                double trueForIds =
//                if(Boolean.parseBoolean(entry.getValue() + "")){
//                    tureForIds.push(id);
//                }else{
//                    falseForIds.push(id);
//                }
//            }

//            gridCheckData.checkTrueForIds(this.request,table.get("gridId")+"", (String[]) tureForIds.toArray());
//            gridCheckData.checkFalseForIds(this.request,table.get("gridId")+"", (String[]) falseForIds.toArray());

            gridUpdateData.updateRow(this.request, table.get("gridId") + "", null);
            gridUpdateData.insertRow(this.request, table.get("gridId") + "", null);
            gridUpdateData.deleteRow(this.request, table.get("gridId") + "", null);

        }
    }

    public static void main(String[] args) {
//        DTOJwlr dtoJwlr = new DTOJwlr();
        String formGrid = "[{\"gridId\":\"price-grid\",\"data\":[],\"checkData\":{\"2\":true}}]";

        new DTOJwlr(null).updateGridData(new GridUpdateData() {
            @Override
            public void insertRow(HttpServletRequest request, String tableName, Map<String, Object> row) {

            }

            @Override
            public void updateRow(HttpServletRequest request, String tableName, Map<String, Object> row) {

            }

            @Override
            public void deleteRow(HttpServletRequest request, String tableName, Map<String, Object> row) {

            }
        });
    }
}
















